package mailHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import lib.Propertie;

public class EmailHelper {
	Propertie propertie;
	
public EmailHelper () throws IOException, InvalidFormatException{
	propertie = Propertie.ladepropertieInstanz();
}

    public void sendeMail(String pfadName, String pdfName, String empfaenger, String datum) throws AddressException, MessagingException {
        ArrayList<String> empfaengerliste = new ArrayList<String>();
        // Properties laden und empfaenger nach mehreren Emailadressen spliten
            Scanner s = new Scanner(empfaenger);
            s.useDelimiter("\\,");
            while (s.hasNext()) {
                if (s.hasNext()) {
                    empfaengerliste.add(s.next());
                }
            }
            s.close();
            
            Session session = Session.getDefaultInstance(propertie,
                    new javax.mail.Authenticator() {

                        protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                            // return new
                            // PasswordAuthentication(Konstanten.EMAIL_ABSENDER
                            // ,Konstanten.EMAIL_PASSWORD);
                            return new javax.mail.PasswordAuthentication(propertie.getPropertieEintrag("EMAILABSENDER"), propertie.getPropertieEintrag("EMAILPASSWORT"));
                        }
                    });
            // zusammenbau der Message und verschicken

            for (int i = 0; i < empfaengerliste.size(); i++) {
        
                MimeMessage message = new MimeMessage(session);
                message.setFrom(new InternetAddress(propertie.getPropertieEintrag("EMAILABSENDER")));
               // message.setRecipients(RecipientType.TO, new Address)
                message.setRecipients(RecipientType.TO,
                        InternetAddress.parse(empfaengerliste.get(i)));
                message.setSubject("Wartungsschein vom "+ datum );
                
                //logdatei
                MimeMultipart content = new MimeMultipart();
                FileDataSource fileDataSource = new FileDataSource(pfadName+"\\" + pdfName);
               MimeBodyPart loggerdatei = new MimeBodyPart();
                loggerdatei.setDataHandler(new DataHandler(fileDataSource));
                loggerdatei.setFileName(pdfName+".pdf");
                
                content.addBodyPart(loggerdatei);
                message.setContent(content);

                Transport.send(message);
            }
    }

}
